﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NFine.Application.Biz;
using NFine.Application.SystemManage;
using NFine.Code;
using NFine.Domain.Entity.SystemManage;

namespace NFine.Web.Areas.BizManage.Controllers
{
    public class UserGiveController : ControllerBase
    {
        
        //
        // GET: /BizManage/UserGive/

        private UserGiveApp userGiveApp = new UserGiveApp();
        private UserApp userApp = new UserApp();



        public ActionResult Index()
        {
            return View();
        }

        
        public  ActionResult Form()
        {
            
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJson(Pagination pagination, int Type, string GiveTimeStart, string GiveTimeEnd)
        {
            var u = OperatorProvider.Provider.GetCurrent();
            var mid = string.Empty;
            if (u != null && (u.RoleId == Configs.GetValue("MemberRoleId") || u.RoleId == Configs.GetValue("TransferCenterRoleId")))
                mid = u.UserId;
            var data = new
            {
                rows =
                userGiveApp.GetList(pagination,Type, GiveTimeStart, GiveTimeEnd, mid),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitForm(string ReceiveUserAccount,decimal GiveCount,string Remarks)
        {
            var u = OperatorProvider.Provider.GetCurrent();
            if(u == null)
                return Error("获取登录信息失败，请重新登录！");

            List<UserEntity> listUser = userApp.GetByAccount(ReceiveUserAccount);
            if (listUser == null || listUser.Count <= 0)
            {
                return Error("收取人账号不存在，请重新输入！");
            }
            else
            {
                int result = userGiveApp.InsertRecord(u.UserId,listUser[0].F_Id, GiveCount, Remarks);
                if (result > 0)
                {
                    return Success("赠送成功！");
                }
                else
                    return Error("赠送失败，请重新提交！");
            }

            
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetReceiveName(string receiveAccount)
        {
            List<UserEntity> list = userApp.GetByAccount(receiveAccount);
            var temp = "";
            if (list.Count > 0)
            {
                temp = list[0].F_RealName;
            }
            var data = new
            {
                receiveName = temp,
            };
            return Content(data.ToJson());
        }

    }
}
